$.sidebarMenu = function (menu) {
    var animationSpeed = 300;

    $(menu).on('click', 'li.first a.first', function (e) {
        $("ul.nav.navbar-nav.clear li").removeClass("current");
        var $this = $(this);
        var checkElement = $this.next();

        if (checkElement.is('.treeview-menu') && checkElement.is(':visible')) {
            checkElement.slideUp(animationSpeed, function () {
                checkElement.removeClass('menu-open');
            });
            checkElement.parent("li.first").removeClass("active");
        }

        //If the menu is not visible
        else if ((checkElement.is('.treeview-menu')) && (!checkElement.is(':visible'))) {
            //Get the parent menu
            var parent = $this.parents('ul.second').first();
            //Close all open menus within the parent
            var ul = parent.find('ul.second:visible').slideUp(animationSpeed);
            //Remove the menu-open class from the parent
            ul.removeClass('menu-open');
            //Get the parent li
            var parent_li = $this.parent("li.first");

            //Open the target menu and add the menu-open class
            checkElement.slideDown(animationSpeed, function () {
                //Add the class active to the parent li
                checkElement.addClass('menu-open');
                parent.find('li.first.active').removeClass('active');
                parent_li.addClass('active');
            });
        }
        //if this isn't a link, prevent the page from being redirected
        if (checkElement.is('.treeview-menu')) {
            e.preventDefault();
        }
        $("ul.first.sidebar-menu li.first.treeview").eq($(this).parents("li.first").index()).addClass("active").siblings().removeClass("active");
    });

    var timeoutID = null;
    $(menu).on('click', 'a.second', function (e) {
        clearTimeout(timeoutID);
        var $this = $(this);
        var checkElement = $this.next();
        timeoutID = window.setTimeout(function () {
            $("ul.nav.navbar-nav.clear li").removeClass("current");

            $(".sidebar-menu li.first.treeview").eq($this.parents("li.first").first().index()).addClass("active").siblings().removeClass("active");
            $this.parents("li.second").addClass("active").siblings().removeClass("active");
            console.log($this.parents("li.second").index());
        }, 300);

    });

    $(menu).on('dblclick', 'a.second', function (e) {
        clearTimeout(timeoutID);
        var $this = $(this);
        var checkElement = $this.parent().next();
        $("ul.nav.navbar-nav.clear li").removeClass("current");

        console.log("doubleclick");
        console.log(checkElement);
        if (checkElement.is('.treeview-menu') && checkElement.is(':visible')) {
            console.log("visible");
            checkElement.slideUp(animationSpeed, function () {
                checkElement.removeClass('menu-open');
            });
            checkElement.parent("li.second").removeClass("active");
        }

        //If the menu is not visible
        else if ((checkElement.is('.treeview-menu')) && (!checkElement.is(':visible'))) {
            console.log("invisible");
            //Get the parent menu
            var parent = $this.parents('ul.third').first();
            //Close all open menus within the parent
            var ul = parent.find('ul.third:visible').slideUp(animationSpeed);
            //Remove the menu-open class from the parent
            ul.removeClass('menu-open');
            //Get the parent li
            var parent_li = $this.parent("li.second");

            //Open the target menu and add the menu-open class
            checkElement.slideDown(animationSpeed, function () {
                //Add the class active to the parent li
                checkElement.addClass('menu-open');
                parent.find('li.second.active').removeClass('active');
                parent_li.addClass('active');
            });
        }
        //if this isn't a link, prevent the page from being redirected
        if (checkElement.is('.treeview-menu')) {
            e.preventDefault();
        }

        $(".sidebar-menu li.first.treeview").eq($(this).parents("li.first").first().index()).addClass("active").siblings().removeClass("active");
        $(this).parents("li.second").addClass("active").siblings().removeClass("active");
        // console.log($(this).parents("li.second").index());
    });

    $(menu).on('click', 'a.third', function (e) {
        var $this = $(this);
        var checkElement = $this.next();
        $("ul.nav.navbar-nav.clear li").removeClass("current");

        $(".sidebar-menu li.second.treeview").eq($this.parents("li.second").first().index()).addClass("active").siblings().removeClass("active");
        $this.parents("li.third").addClass("active").siblings().removeClass("active");
        console.log($this.parents("li.third").index());

    });
}
